#---收集数据 并把计算结果保存在文件中---
def CollectClusterData(database, instruments, datetime):

    clusterData = []
    i = 0
    for instrument in instruments:
        i = i + 1
        symbol = instrument.symbol

        Gadget.__Print__("Collecting Cluster Data : " + str(i) + " " + symbol + " " + str(datetime))
        capSeries = database.getDataSeries(symbol + "_Capitalization_7776000_Dynamic_FundamentalFactorValue",None,None)
        cap = capSeries.get(datetime)
        if cap == None:
            continue
        capDateTime = cap["DateTime"]
        capValue = cap["Value"]

        bookToMarketSeries = database.getDataSeries(symbol + "_PriceBook_7776000_Calc_Single_Dynamic_FundamentalFactorValue",None,None)
        bookToMarket = bookToMarketSeries.get(datetime)
        if bookToMarket == None:
            continue
        bookToMarketDateTime = bookToMarket["DateTime"]
        bookToMarketValue = bookToMarket["Value"]
        if bookToMarketValue == 0:
            continue
        bookToMarketValue = 1 / bookToMarketValue

        #---Save Valid Instrument / Value---
        obj = []
        obj.append(instrument)
        value = []
        value.append(capValue)
        value.append(bookToMarketValue)
        obj.append(value)
        clusterData.append(obj)
    #---大规模计算消耗时间，可以把计算结果进行保存，以备以后使用 ---
    np.save("clusterData_" + Gadget.__ToDateTimeString2__(datetime) +".npy",clusterData)
    #f = open("validInstruments_" + Gadget.__ToDateTimeString2__(datetime) +".txt", 'wb')
    #pickle.dump(validInstruments, f)
    #f.close()
    return clusterData


def CollectFactorValues(database, instruments, datetime2):

    profileData = {}
    i = 0;
    for instrument in instruments:
        i = i+1
        symbol = instrument.symbol
        #if symbol != "001979.SZ":
        #    continue

        datetime1 = datetime(2000,1,1,0,0,0)
        fundamentals = database.find("Fundamental",instrument.symbol + "_Fundamental",datetime1, datetime2)
        values = []
        print("Collecting Factor Value Data : " + str(i) + " " + symbol  + " " + str(datetime2) + " # " + str(fundamentals.__len__()))

        #---ROE Factor---
        roeSeries = database.getDataSeries(symbol + "_ROE_7776000_Rolling_FundamentalFactorValue",datetime1,datetime2)
        roe = roeSeries[roeSeries.count()-1]
        if roe == None:
            print("Faild to Collect ROE")
            continue
        value = roe["Value"]
        values.append(value)

        #---EPS Factor---
        factorSeries = database.getDataSeries(symbol + "_EPS_7776000_Calc_Single_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect EPS")
            continue
        value = factor["Value"]
        values.append(value)

        #---PE Factor---
        factorSeries = database.getDataSeries(symbol + "_PriceEarning_7776000_Calc_Rolling_Dynamic_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect PE")
            continue
        value = factor["Value"]
        values.append(value)

        #---PB Factor---
        factorSeries = database.getDataSeries(symbol + "_PriceBook_7776000_Calc_Single_Dynamic_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect PB")
            continue
        value = factor["Value"]
        values.append(value)

        #---Rev Growth Factor---
        factorSeries = database.getDataSeries(symbol + "_Growth-TotalRevenue_7776000_4o4_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect Rev Growth")
            continue
        value = factor["Value"]
        values.append(value)

        #---Ni Growth Factor---
        factorSeries = database.getDataSeries(symbol + "_Growth-NetIncome1_7776000_4o4_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect NI Growth")
            continue
        value = factor["Value"]
        values.append(value)

        #---CashROIC Factor---
        factorSeries = database.getDataSeries(symbol + "_CashROIC_7776000_Rolling_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect Cash ROIC")
            continue
        value = factor["Value"]
        values.append(value)

        #---FCFToOptIncome Factor---
        factorSeries = database.getDataSeries(symbol + "_FCFToOptIncome_7776000_Rolling_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect FCFToOptIncome")
            continue
        value = factor["Value"]
        values.append(value)

        #---DebtToAsset Factor---
        factorSeries = database.getDataSeries(symbol + "_DebtToAsset_7776000_Rolling_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect DebtToAsset")
            continue
        value = factor["Value"]
        values.append(value)

        #---Cap Factor---
        factorSeries = database.getDataSeries(symbol + "_Capitalization_7776000_Dynamic_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect Cap")
            continue
        value = factor["Value"]
        values.append(value)

        #---FCF To Debt Factor---
        factorSeries = database.getDataSeries(symbol + "_FCFToDebt_7776000_Rolling_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect FCF To Debt")
            continue
        value = factor["Value"]
        values.append(value)

        #---Turnover Factor---
        factorSeries = database.getDataSeries(symbol + "_TurnOver_7776000_Rolling_FundamentalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect Turnover")
            continue
        value = factor["Value"]
        values.append(value)

        #---Bias Factor---
        factorSeries = database.getDataSeries(symbol + "_Bias_86400_20_TechnicalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect Bias")
            continue
        value = factor["Value"]
        values.append(value)

        #---RSI Factor---
        factorSeries = database.getDataSeries(symbol + "_RSI_86400_14_TechnicalFactorValue",datetime1,datetime2)
        factor = factorSeries[factorSeries.count()-1]
        if factor == None:
            print("Faild to Collect RSI")
            continue
        value = factor["Value"]
        values.append(value)

        Gadget.__Print__("Collected : " + instrument.symbol + " " + str(datetime2) + " " + str(values))
        #---Create a Y-entry---
        profileData[instrument.symbol] = values

    np.save("factorValues_" + ToDateString2(datetime2) +".npy",profileData)
    #f = open("ProfileData_" + Gadget.__ToDateTimeString2__(datetime) +".txt", 'wb')
    #pickle.dump(validInstruments, f)
    #f.close()


def CollectAlphaData(database, instruments, datetime1, datetime2):
    alpha = {}
    bmSeries = database.getDataSeries("000300.SH_Time_86400_Bar",datetime1,datetime2)
    datetime1HasData = bmSeries.getDateTime(0)
    datetime2HasData = bmSeries.getDateTime(bmSeries.count() - 1)
    firstBar = bmSeries.get(datetime1HasData)
    lastBar = bmSeries.get(datetime2HasData)
    bm = lastBar["Close"] / firstBar["Open"] - 1

    i = 0
    for instrument in instruments:
        symbol = instrument.symbol
        Gadget.__Print__("Collecting Alpha Data : " + str(i) + " " + symbol + " " + Gadget.__ToDateTimeString2__(datetime1) + "-" + Gadget.__ToDateTimeString2__(datetime2))
        barSeries = database.getDataSeries(symbol + "_Time_86400_Bar",datetime1,datetime2)
        firstBar = barSeries.get(datetime1HasData)
        lastBar = barSeries.get(datetime2HasData)

        if firstBar == None or lastBar == None:
            continue

        #---停牌股票不计算Alpha---
        if firstBar["TradeStatus"] == "停牌一天" or lastBar["TradeStatus"] == "停牌一天":
            continue

        ret = lastBar["Values"]["BClose"] / firstBar["Values"]["BOpen"] - 1
        alphaValue = ret - bm

        i = i + 1
        Gadget.__Print__("Alpha : " + str(alphaValue))

        #---Create a Y-entry---
        alpha[instrument.symbol] = alphaValue

    #---Export to File---
    np.save("alphas_" + ToDateString2(datetime1) + "-" + ToDateString2(datetime2) +".npy",alpha)
    #f = open("alphas_" + Gadget.__ToDateTimeString2__(datetime1) + "-" + Gadget.__ToDateTimeString2__(datetime2) +".txt", 'wb')
    #pickle.dump(alpha, f)
    #f.close()
    return alpha


def CollectTrailing(database, instruments, datetime1, datetime2):
    trailingBySymbol = {}
    for instrument in instruments:
        symbol = instrument.symbol
        trailing = CalcTrailing(symbol,database,datetime1, datetime2)
        trailingBySymbol[symbol] = trailing


def CollectAlphaData2(database,instruments, bars, datetime1, datetime2):
    alpha = {}
    bmSeries = database.getDataSeries("000300.SH_Time_86400_Bar",datetime1,datetime2)
    datetime1HasData = bmSeries.getDateTime(0)
    datetime2HasData = bmSeries.getDateTime(bmSeries.count() - 1)
    firstBar = bmSeries.get(datetime1HasData)
    lastBar = bmSeries.get(datetime2HasData)
    bm = lastBar["Close"] / firstBar["Open"] - 1

    i = 0
    for instrument in instruments:
        symbol = instrument.symbol
        print("Collecting Alpha Data : " + str(i) + " " + symbol + " " + Gadget.__ToDateTimeString2__(datetime1) + "-" + Gadget.__ToDateTimeString2__(datetime2))
        barSeries = database.getDataSeries(symbol + "_Time_86400_Bar",None,None)
        firstBar = barSeries.get(datetime1HasData)
        lastBar = barSeries.get(datetime2HasData)

        if firstBar == None or lastBar == None:
            continue

        #---停牌股票不计算Alpha---
        if firstBar["TradeStatus"] == "停牌一天" or lastBar["TradeStatus"] == "停牌一天":
            continue

        ret = lastBar["Values"]["BClose"] / firstBar["Values"]["BOpen"] - 1
        alphaValue = ret - bm

        i = i + 1
        Gadget.__Print__("Alpha : " + str(alphaValue))

        #---Create a Y-entry---
        alpha[instrument.symbol] = alphaValue

    #---Export to File---
    np.save("alphas_" + ToDateString2(datetime1) + "-" + Gadget.ToDateString2(datetime2) +".npy",alpha)
    #f = open("alphas_" + Gadget.__ToDateTimeString2__(datetime1) + "-" + Gadget.__ToDateTimeString2__(datetime2) +".txt", 'wb')
    #pickle.dump(alpha, f)
    #f.close()
    return alpha
